// pages/drugApplication/index.js
const app = getApp()
import {
  ApiGetMedicineList
} from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    $path: app.globalData.$path,
    medicine: [],
    ops: {
      checkResult: []
    },
    sum: 0,
    finish: false,
    loading: false,
    jiezhen: false,
    params: {
      size: 10,
      page: 1,
      finish: false,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMedicList()
  },
  onLoadMore(){
    if (this.data.loading || this.data.params.finish) return
    this.data.params.page += 1
    this.getMedicList()
  },
  handleTap() {

  },

  onSearch(e){
    /* let arr = []
     this.data.medicineArr.forEach(item=>{
    if (item.name.includes(e.detail.toString())) {
  arr.push(item)
    } 
    })
    if(arr.length>0){
      this.setData({
        'searData':arr
      })
    }else {
      this.setData({
      })
    } */
    this.setData({
      'params.page':1
    })
    this.getMedicList(e.detail)
  },
   mergeArrays(arr1, arr2) {
    // 创建一个Set存储arr1中的所有id
    const idsInArr1 = new Set(arr1.map(item => item.id));
    
    // 合并结果：arr1的所有元素 + arr2中id不在arr1中的元素
    return [
        ...arr1,
        ...arr2.filter(item => !idsInArr1.has(item.id))
    ];
},
  getMedicList(name) {
let that = this
    let DoctorCommentListDat=[]
    wx.showLoading({
      title: '加载中',
    })
    ApiGetMedicineList({
      name,
      type: '非处方药',
      ...this.data.params
    }).then(res => {
      if(name&&res.data==0){
        wx.showToast({
          title: '暂无该药品',
          icon:'error'
        })
        return;
      }
      let dataRe=null
      let data = null
      //是否显示选择数量
      res.data.forEach(item => {
        //判断一下，如果之前的medicineArr里面有这个值的话 就去现在的值吧之前的值给删除掉
        item.checkStep = false
        item.count = '1'
        if(this.data.medicineArr&&this.data.medicineArr.length>0){
           dataRe = this.data.medicineArr.filter(item=>{
            if(item.checkStep){
              return item
            }
          })
        }
      })
      if(dataRe && dataRe.length>0){
      data = that.mergeArrays(dataRe,res.data)
      }else {
      data = res.data
      }
       
      DoctorCommentListDat = this.data.params.page === 1 ? data : this.data.medicineArr.concat(data)
      DoctorCommentListDat=[...new Set(DoctorCommentListDat)]
      this.data.params.finish = DoctorCommentListDat.length >= res.page.total
      this.setData({
        medicineArr:DoctorCommentListDat,
        loading: false
      })
    }).finally(_=>{
      wx.hideLoading()
    })
  },

  //切换药品
  onChangecCheckBox(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      [`medicineArr[${index}].checkStep`]: e.detail
    })
  /*   this.getSumData()  */
  },
/*   setCheckData(id) {
    const medicineArr = this.data.medicineArr.map(item => {
      // 如果数组一中包含当前元素的 id，则修改 ischeck 为 true
      if (id.includes(item.id.toString())) {
        item.checkStep = true;
      } 
      return item;
    })
    this.setData({
      medicineArr
    })
  }, */

  //切换步进
  onChangeStep(e) {
    let index = e.currentTarget.dataset.index
    let oldPrice = this.data.medicineArr[index].price
    this.data.medicineArr[index].count = e.detail
    this.data.medicineArr[index].sumPrice = Number(e.detail) * Number(oldPrice)
  /*   this.getSumData() */
  },

  //汇总合计数据
  getSumData() {
    this.data.medicine = []
    this.data.sum = 0
    this.data.medicineArr.forEach(item => {
      if (item) {
        this.data.sum += item.sumPrice ? item.sumPrice : Number(item.price)
        this.data.medicine.push({
          id: item.id,
          count: item.count,
          price: item.price,
          name: item.name
        })
      }
    })
    this.setData({
      sum: this.data.sum,
      medicine: this.data.medicine
    })
  },

  //去缴费

  onClickButton() {
    this.data.medicine = []
    this.data.sum = 0
    this.data.medicineArr.forEach(item => {
      if (item.checkStep) {
        this.data.sum += item.sumPrice ? item.sumPrice : Number(item.price)
        this.data.medicine.push({
          id: item.id,
          count: item.count,
          price: item.price,
          name: item.name
        })
      }
    })
    this.setData({
      sum: this.data.sum,
      medicine: this.data.medicine
    })


    wx.navigateTo({
      url: `/pages/payment/index?medicine=${JSON.stringify(this.data.medicine)}&&sum=${this.data.sum}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})